﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AntiObesity.DatabaseAccess;

namespace AntiObesity.Base.Managers
{
    public static partial class BodyHistoryManager
    {
        public static BodyHistory GetLatestHistory(Guid userID)
        {
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn, String.Format(
                    "SELECT * FROM [BodyHistories] WHERE [UserID]='{0}' AND [Date] IN " +
                    "(SELECT MAX(Date) FROM [BodyHistories] WHERE [UserID]='{0}')"
                    , userID));
                if (reader.Read())
                {
                    return CreateBodyHistory(reader);
                }
            }
            return null;
        }

        public static BodyHistory GetFirstHistory(Guid userID)
        {
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn, String.Format(
                    "SELECT * FROM [BodyHistories] WHERE [UserID]='{0}' AND [Date] IN " +
                    "(SELECT Min(Date) FROM [BodyHistories] WHERE [UserID]='{0}')"
                    , userID));
                if (reader.Read())
                {
                    return CreateBodyHistory(reader);
                }
            }
            return null;
        }

        public static void PersistHistory(Guid userID, float weight, float height)
        {
            var latestHistory = GetLatestHistory(userID);
            if (latestHistory == null || latestHistory.HasDifferentData(weight, height))
            {
                Persist(new BodyHistory
                {
                    UserID = userID,
                    Date = DateTime.Now,
                    Weight = weight,
                    Height = height
                });
            }
            // Otherwise, do nothing
        }

        public static BodyHistory FindUserHistoryByUserAndDate(Guid userID, DateTime date)
        {
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn,
                    String.Format("SELECT * FROM [BodyHistories] WHERE [UserID]='{0}' AND [Date]='{1}'",
                    userID,
                    date));
                if (reader.Read())
                {
                    return CreateBodyHistory(reader);
                }
            }
            return null;
        }

        public static List<BodyHistory> GetAll(Guid userID)
        {
            var result = new List<BodyHistory>();
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn,
                    String.Format("SELECT * FROM [BodyHistories] WHERE [UserID]='{0}' ORDER BY [Date] DESC ", userID));
                while (reader.Read())
                {
                    result.Add(CreateBodyHistory(reader));
                }
            }
            return result;
        }

    }
}
